package cn.topevery.garden.framework.util;

import hw.topevery.basis.entity.po.SysDept;
import hw.topevery.basis.entity.po.SysPrivilege;
import hw.topevery.basis.entity.po.SysUser;
import hw.topevery.basis.framework.enums.DeptTypeEnum;
import hw.topevery.basis.runtime.UserRuntime;
import hw.topevery.framework.enums.MapTypeEnum;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;

import java.util.List;


/**
 * 当前登录用户工具类
 *
 * @date: 2020/6/6
 */
public class CurrentUserUtil {
    /**
     * 获取当前登录用户
     *
     * @return
     */
    public static SysUser getUser() {
        return UserRuntime.getSysUser();
    }

    /**
     * 获取当前登录用户所属部门
     *
     * @return
     */
    public static SysDept getDept() {
        return UserRuntime.getSysDept();
    }

    /**
     * 获取当前登录用户所有权限
     *
     * @return
     */
    public static List<SysPrivilege> getPrivilegeList() {
        return UserRuntime.getSysPrivilege();
    }

    /**
     * 当前登录用户是否为企业用户
     *
     * @return
     */
    public static boolean isCompany() {
        SysDept currentDept = getDept();
        if (ObjectUtils.isEmpty(currentDept) || ObjectUtils.isEmpty(currentDept.deptType)) {
            return false;
        }
        return DeptTypeEnum.COMPANY.getCode().equals(currentDept.deptType) || DeptTypeEnum.TEAM.getCode().equals(currentDept.deptType);
    }

    /**
     * 当前登录用户是否为项目组用户
     *
     * @return
     */
    public static boolean isTeam() {
        SysDept currentDept = getDept();
        return currentDept != null && DeptTypeEnum.TEAM.getCode().equals(currentDept.deptType);
    }

    /**
     * 当前登录账号是否属于街道账号
     *
     * @return
     */
    public static boolean isStreet() {
        SysDept currentDept = getDept();
        return currentDept != null && MapTypeEnum.STREET.getMapCode().equals(currentDept.mapType);
    }

    /**
     * 当前登录账号是否属于区账号
     *
     * @return
     */
    public static boolean isDistrict() {
        SysDept currentDept = getDept();
        return currentDept != null && MapTypeEnum.DISTRICT.getMapCode().equals(currentDept.mapType);
    }

    /**
     * 当前登录账号是否为管理员
     *
     * @return
     */
    public static boolean isAdmin() {
        return 1 == getUser().userIsAdmin;
    }

    /**
     * 是否包含权限
     *
     * @param privilegeId 权限ID
     * @return
     */
    public static boolean containPrivilege(String privilegeId) {
        List<SysPrivilege> privilegeList = getPrivilegeList();
        if (1 == getUser().userIsAdmin) {
            return true;
        }
        if (CollectionUtils.isNotEmpty(privilegeList)) {
            for (SysPrivilege item : privilegeList) {
                if (privilegeId.equals(item.privilegeId)) {
                    return true;
                }
            }
        }
        return false;
    }
}
